﻿using AutoMapper;
using MediatR;
using Mysqlx.Crud;
using NPOI.SS.Formula.Functions;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using System.Security.Cryptography;
using THE_WIND_RISES.Common.Domans.BasicInformation;
using THE_WIND_RISES.Common.Domans.BasicInformation.Log;
using THE_WIND_RISES.Common.Dtos;

namespace Enterprise_Set.API.Application.Commands.Week1.Log_CR
{
    /// <summary>
    /// 添加日志
    /// </summary>
    public class AddLogHandler : IRequestHandler<AddLogCommand, Result>
    {
        public AddLogHandler(IBaseRepository<UserLog> logRepository, IMapper mapper)
        {
            LogRepository = logRepository;
            this.mapper = mapper;
        }

        public IBaseRepository<UserLog> LogRepository { get; set; }
        public IMapper mapper { get; set; }
        public async Task<Result> Handle(AddLogCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result() { Code = (int)CommonErrorCodes.Added };
                var HandleTime = DateTime.Now;
                var AddLogData = $"操作人:{request.Handle};操作行为:{request.HandleContext};操作时间:{HandleTime}";
                var sql = "insert into UserLog values(@UserLogId,@UserLogData,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete)";
                var AddNum = LogRepository.Add(sql, new
                {
                    UserLogId = request.UserLogId,
                    UserLogData = AddLogData,
                    Handle = request.Handle,
                    HandleContext = request.HandleContext,
                    HandleTime = HandleTime,
                    Pid = request.Pid,
                    IsDelete = request.IsDelete
                });
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
